<?php

namespace app\common\model;

use basic\ModelBasic;

class Routes extends ModelBasic
{
	use \traits\ModelTrait;

    /** 商品类型 */
    public static $route_type = [
        1 => '长线',
        2 => '短线',
        3 => '周边游',
    ];

	 /**
     * 列表
     * @Author   FW
     * @DateTime 2020-09-24
     * @Email    123763712@qq.com
     * @param    [type]
     * @return   [type]
     */
    public static function getList($where){
        $count = self::setWhere($where)->count();
        $data  = [];
        if ( $count == 0 ) {
            return compact('data','count');
        }

        $data=self::setWhere($where,'m')
        ->field("m.*,m.route_name as title")
        ->page($where['page'] ?? 1,$where['limit'] ?? 10)
        ->select();

        if ( !empty($data) ) {
            $data = $data->toArray();
            $data = static::process_list($data);
        }

        return compact('data','count');
    }

    /**
     * [getOne description]
     * @param    [type]           $id [description]
     * @return   [type]               [description]
     */
    public static function getOne ($id)
    {
        $item = self::alias('m')->where('m.id',$id)
            ->field("m.*")
            ->with(['desposit','details'])
            ->find();

        if ( empty($item) ) {
            return [];
        }

        $item = $item->toArray();
        return static::process_list([$item])[0];
    }

    /**
     * 定金配置
     */
    public function desposit()
    {
        return $this->hasOne('RoutesDespositCfg','route_id');
    }

    /**
     * 详情配置（分模块）
     */
    public function details()
    {
        return $this->hasMany('RoutesDetails','route_id')->order('sort ASC');
    }

     /**
     * 处理列表数据
     * @param    array            $list [description]
     * @return   [type]                 [description]
     */
    public static function process_list (array $list)
    {
        foreach ($list as $key => &$value) {
            $value['route_type_text'] = static::$route_type[$value['route_type']] ?? '-';;
            $value['status_text']      = static::$common_status[$value['status']] ?? '-';
            $value['label']  = json_decode($value['label']);
            $value['albums'] = json_decode($value['albums']);
        }
        return $list;
    }

	/**
	 * 设置where条件
	 * @Author   FW
	 * @DateTime 2021-01-12
	 * @Email    123763712@qq.com
	 * @param    [type]           $where [description]
	 * @param    string           $alias [description]
	 * @param    [type]           $model [description]
	 */
	public static function setWhere($where,$alias='m',$model=null){
        $model = $model===null ? new self() : $model;

        $model->alias($alias);

        if ( isset($where['route_type']) && $where['route_type'] > 0 ) {
            $model->where(sprintf('%s.route_type = %d' , $alias , $where['route_type']));
        }

        if ( isset($where['route_name']) && $where['route_name'] != '' ) {
           $model = $model->where('route_name','LIKE' , '%' . $where['route_name'] . '%');
        }

        if ( isset($where['recomand_id']) && $where['recomand_id'] > 0 ) {
            $rels = RelRecomandRoutes::where('recomand_id',$where['recomand_id'])->column('route_id');
            $model = $model->where('id','IN',$rels);
        }

        if ( isset($where['order']) && $where['order'] != '' ) {
            $model->order(sprintf('%s.%s' , $alias, $where['order']));
        } else {
            $model->order(sprintf('%s.id ASC' , $alias));
        }

        return $model;
    }

     /**
     * 主数据保存后回调
     * @Author   FW
     * @DateTime 2021-08-10
     * @Email    123763712@qq.com
     * @param    [type]           $prk_id [description]
     * @param    array            $data   [description]
     * @return   [type]                   [description]
     */
    public static function afterSave ( $prk_id , array $data )
    {
        ## 详情
        if ( isset($data['details']) ) {
            $res = (new RoutesDetails)->isUpdate(true)->saveAll($data['details']);
            if ( $res === false ) throw new \Exception('保存详情失败');
        }

        ## 定金配置
        if ( $data['support_deposit'] == 1 && isset($data['desposit']) ) {
            $data['desposit']['route_id'] = $prk_id;
            $res = RoutesDespositCfg::saveOne($data['desposit']);
            if ( $res === false ) throw new \Exception('保存定金配置失败');
        }
        return true;
    }
}//Class End
